import qs from 'qs'
import { getSession } from '../libs/cache'
import { errorResponse } from './response'
import request from './request'
import baseURL from './url'

const axios = request

// 安卓打包ip配置【勿动】
const uri = baseURL.dip

// 检查普通回调的请求
const checkData = (method,params,isFile) => {
    if(method === "get" || method === "delete") params = {params: params}
    else params = qs.stringify(params)
    
//  if(isFile) axios.defaults.headers['Content-Type'] = 'multipart/form-data;charset=utf-8'
//  else if(!isFile && (method === "post" ||  method === "put")) axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'
//  else axios.defaults.headers['Content-Type'] = 'application/json;charset=UTF-8'
    
    return params
}

// 检查直接返回的请求
const checkConfig = (config) => {
    if(config.method === "post"){
        config.data = qs.stringify(config.data)
        axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'
    }else{
        //config.data = {params: config.data}
        config.params = config.data
        axios.defaults.headers['Content-Type'] = 'application/json;charset=UTF-8'
    }
    return config
}

// 安卓调取数据接口方法
const androidApi = (action,method,param,resolve,reject) => {
	let params = {
		url: uri + action,
		method: method,
		params: param
	}
	console.log('安卓数据Api',uri + action)
    nativeApi.nativeMethod("http","httpRequest",params,res => {
    	return resolve(res)
    },err => {
    	errorResponse(err)
    })
}

// 安卓调取上传文件接口方法
const androidFile = (action,method,param,filePath,resolve,reject) => {
	let params = {
		url: uri + action,
		file: filePath,
		isImage: true,
		params: param
	}
	console.log('安卓上传Api',uri + action)
    nativeApi.nativeMethod("http","uploadFile",params,res => {
    	return resolve(res)
    },err => {
    	errorResponse(err)
    })
}

// 前端静态文件ip配置【照片、音频、视频】
export const fileUrl = (fileUri)=>{
	let imgUrl = ''
	if(process.env.NODE_ENV === 'production'){
		imgUrl = uri+'/zuul/imgurl'+fileUri+'?access_token='+getSession("token").split(' ')[1]
	}else{
		imgUrl = uri+'/zuul/imgurl'+fileUri+'?access_token='+getSession("token").split(' ')[1]
	}
	return imgUrl
}

// 静态资源Json文件请求
export const getJson = (action)=>{
    return new Promise((resolve, reject) => {
		axios.get(action).then(res => {
	        resolve(res)
	    }).catch(err => {
	        reject(err)
	    })
    })
}

// 数据回调请求
export const callData = (action,method,params)=>{
	action = '/zuul'+ action
    let param = params
    params = checkData(method,params,false)
    return new Promise((resolve, reject) => {
    	if(window.Cordova){
    		return androidApi(action,method,param,resolve,reject)
	    }else{
			axios[method](action,params).then(res => {
	            resolve(res)
	        }).catch(err => {
	            reject(err)
	        })
	    }
    })
}

// 文件回调请求
export const fileData = (action,method,params,filePath)=>{
	action = '/zuul'+ action
    let param = params
	let config = {
        headers: {'Content-Type': 'multipart/form-data'}
	}
//  params = checkData(method,params,true)
    return new Promise((resolve, reject) => {
    	if(window.Cordova){
	    	return androidFile(action,method,param,filePath,resolve,reject)
	    }else{
			axios[method](action,params,config).then(res => {
	            resolve(res)
	        }).catch(err => {
	            reject(err)
	        })
	    }
    })
}

// 数据直接请求
export const apiData = (config)=>{
	config.url = '/zuul'+ config.url
    config = checkConfig(config)
    return axios.request(config)
}

export const all = (allConfig, cb) => {
	allConfig.url = '/zuul'+ allConfig.url
	axios.all(allConfig).then(
		axios.spread(function (...data) {
			cb(data)
		})
	)
}
